<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.cqggrw.util.*" %>
<%
	request.setCharacterEncoding("UTF-8");
	String commond = request.getParameter("commond");
	String id = null;
	String texName = null;
	int falg = 0;
	if("add".equals(commond)) {
		String name = request.getParameter("txtMinCategory");
		if(name != null) {
			Connection conn = null;
			PreparedStatement ps = null;
			
			String sql = "insert into tbl_category(name) values(?);";
			int i = 0;
			try {
				conn = DBUtil.getConn();
				ps = conn.prepareStatement(sql);
				ps.setString(1, name);
				i = ps.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				DBUtil.close(conn, ps, null);
			}
		}
		falg = 1;
	}else if("modify".equals(commond)) {
		falg = 2;
		id = request.getParameter("id");
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "SELECT * FROM tbl_category where id = ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, Integer.parseInt(id));
			rs = ps.executeQuery();

			if (rs.next()) {
				texName = rs.getString("name");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
	} else if("doModify".equals(commond)) {
		falg =3;
		String strId = request.getParameter("id");
		String strName = request.getParameter("txtMinCategory");
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "update tbl_category set name = ? WHERE ID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, strName);
			ps.setInt(2, Integer.parseInt(strId));
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>公共人物网后台管理</title>
<link type="text/css" rel="Stylesheet" href="../Content/tabbed_pages.css" />
<link type="text/css" rel="Stylesheet" href="../Content/Index.css" />
<script type="text/javascript" src="../../jslib/jquery-1.8.0.min.js"></script>
<script type="text/javascript">

function WebForm_OnSubmit() {
	if($('#txtMinCategory').val().length == 0) {
		$('#RequiredFieldValidator1').html('<font color="red">*</font>');
		return false;
	}
	return true;
}

</script>
</head>
<body>
	<form name="form1" method="post" action="CategoryEdit.jsp" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
	<input type="hidden" name="commond" value="<%=falg==2?"doModify":"add" %>" />
	<input type="hidden" name="id" value="<%=falg==2?id:"" %>" />
		<div class="form-div textLeft">
			<h1>
				<span class="action-span"><a href="CategoryList.jsp">分类列表</a>
				</span> <span class="action-span1 fs20">公共人物网 管理中心 </span><span class="action-span1 paddingTop5"> - 分类编辑 </span>
			</h1>
			<div style="clear:both"></div>
		</div>
		<div id="gallery">
			<div class="on" title="CommonInfo">
				<span>分类信息</span>
			</div>
		</div><%=falg==1?"<font color=\"red\">添加成功！</font>":"" %><%=falg==3?"<font color=\"red\">修改成功！</font>":"" %>
		<div id="CommonInfo" class="show">
			<table width="90%" id="general-table" align="center">
				<tr>
					<td>添加分类： <input name="txtMinCategory" type="text" value="<%=texName!=null?texName:"" %>" maxlength="30" id="txtMinCategory" /> <span id="RequiredFieldValidator1"></span></td>
				</tr>
				<tr>
					<td>顶级分类： <select name="ddlMaxCategory" id="ddlMaxCategory" MaxLength="30" style="height:20px;width:100px;">
							<option value="-1">请选择...</option>
							<%
							Connection conn = null;
							PreparedStatement ps = null;
							ResultSet rs = null;
							
							String sql = "SELECT * FROM tbl_category;";
							try {
								conn = DBUtil.getConn();
								ps = conn.prepareStatement(sql);
								rs = ps.executeQuery();

								while (rs.next()) {
									
									out.println("<option value=\""+rs.getInt("id")+"\">"+rs.getString("name")+"</option>");
								}
							} catch (Exception e) {
								e.printStackTrace();
							} finally {
								DBUtil.close(conn, ps, rs);
							}
							%>
							
					</select> &nbsp;</td>
				</tr>
			</table>
		</div>
		<div class="form-div textLeft" style="text-align:center;margin-top:5px;">
			<input type="submit" name="btnSave" value="保存" onclick="" id="btnSave" class="button" />
		</div>
	</form>
</body>
</html>